/// @tags: BinaryGraph
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 5e2 + 1;

struct Person {
  int h;
  char sex;
  std::string style, sport;
} p[maxn];

int n;
int match[maxn];
bool vis[maxn];
std::vector<int> e[maxn];

bool dfs(int u) {
  for (auto v : e[u]) {
    if (!vis[v]) {
      vis[v] = true;
      if (!match[v] || dfs(match[v])) {
        match[v] = u;
        return true;
      }
    }
  }
  return false;
}

int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> p[i].h >> p[i].sex >> p[i].style >> p[i].sport;
    for (int i = 1; i <= n; ++i) {
      e[i].clear();
      if (p[i].sex == 'M')
        for (int j = 1; j <= n; ++j)
          if (p[j].sex == 'F') {
            if (std::abs(p[i].h - p[j].h) <= 40 && p[i].style == p[j].style &&
                p[i].sport != p[j].sport)
              e[i].push_back(j);
          }
    }
    int ans = n;
    memset(match, 0, sizeof(match));
    for (int i = 1; i <= n; ++i) {
      memset(vis, 0, sizeof(vis));
      ans -= dfs(i);
    }
    cout << ans << endl;
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("UVA12083 Guardian of Decency.in", "r", stdin);
  freopen("UVA12083 Guardian of Decency.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
